home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / gdata / tlslite / SessionCache.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  3.0 KB  |  89 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Class for caching TLS sessions.'''
  5. import thread
  6. import time
  7.  
  8. class SessionCache:
  9.     '''This class is used by the server to cache TLS sessions.
  10.  
  11.     Caching sessions allows the client to use TLS session resumption
  12.     and avoid the expense of a full handshake.  To use this class,
  13.     simply pass a SessionCache instance into the server handshake
  14.     function.
  15.  
  16.     This class is thread-safe.
  17.     '''
  18.     
  19.     def __init__(self, maxEntries = 10000, maxAge = 14400):
  20.         '''Create a new SessionCache.
  21.  
  22.         @type maxEntries: int
  23.         @param maxEntries: The maximum size of the cache.  When this
  24.         limit is reached, the oldest sessions will be deleted as
  25.         necessary to make room for new ones.  The default is 10000.
  26.  
  27.         @type maxAge: int
  28.         @param maxAge:  The number of seconds before a session expires
  29.         from the cache.  The default is 14400 (i.e. 4 hours).'''
  30.         self.lock = thread.allocate_lock()
  31.         self.entriesDict = { }
  32.         self.entriesList = [
  33.             (None, None)] * maxEntries
  34.         self.firstIndex = 0
  35.         self.lastIndex = 0
  36.         self.maxAge = maxAge
  37.  
  38.     
  39.     def __getitem__(self, sessionID):
  40.         self.lock.acquire()
  41.         
  42.         try:
  43.             self._purge()
  44.             session = self.entriesDict[sessionID]
  45.             if session.valid():
  46.                 return session
  47.             raise KeyError()
  48.         finally:
  49.             self.lock.release()
  50.  
  51.  
  52.     
  53.     def __setitem__(self, sessionID, session):
  54.         self.lock.acquire()
  55.         
  56.         try:
  57.             self.entriesDict[sessionID] = session
  58.             self.entriesList[self.lastIndex] = (sessionID, time.time())
  59.             self.lastIndex = (self.lastIndex + 1) % len(self.entriesList)
  60.             if self.lastIndex == self.firstIndex:
  61.                 del self.entriesDict[self.entriesList[self.firstIndex][0]]
  62.                 self.firstIndex = (self.firstIndex + 1) % len(self.entriesList)
  63.         finally:
  64.             self.lock.release()
  65.  
  66.  
  67.     
  68.     def _purge(self):
  69.         currentTime = time.time()
  70.         index = self.firstIndex
  71.         while index != self.lastIndex:
  72.             if currentTime - self.entriesList[index][1] > self.maxAge:
  73.                 del self.entriesDict[self.entriesList[index][0]]
  74.                 index = (index + 1) % len(self.entriesList)
  75.                 continue
  76.             break
  77.         self.firstIndex = index
  78.  
  79.  
  80.  
  81. def _test():
  82.     import doctest
  83.     import SessionCache
  84.     return doctest.testmod(SessionCache)
  85.  
  86. if __name__ == '__main__':
  87.     _test()
  88.  
  89.